<template>
	<view>
		<dsl-navbar title="收货地址" isBack bgTopImg='white'></dsl-navbar>
		<view class="address_from">
			<view class="title">收货人</view>
			<input type="text" value="" placeholder="请填写收货人姓名" v-model="addressName" />
		</view>

		<view class="address_from">
			<view class="title">手机号码</view>
			<input type="text" value="" placeholder="手机号码" v-model="addressTel" />
		</view>
		<view class="address_from">
			<view class="title">所在地址</view>
			<picker mode="region" @change="RegionChange" :value="region">
				<view class="picker">
					{{region[0]}}，{{region[1]}}，{{region[2]}}
				</view>
			</picker>
		</view>
		<view class="address_from">
			<view class="title">详情地址</view>
			<input type="text" value="" placeholder="详情地址" v-model="addressData" />
		</view>
		<view class=" bg-white padding  margin-top-sm">
			<view class="rowsb">
				<view class="title text-bold text-black">设置为默认地址</view>
				<switch :checked="isChecked" @change="changeAddressType" />
			</view>
			<view class="" style="font-size: 20rpx;">提醒：每次下单会默认推荐使用该地址</view>
		</view>
		<view class="rowsc">
			<view class="address_submit" @click="getAddress(1)">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAddress,
		set_address,
		get_address_data,
		set_edit_addrees

	} from '@/common/path.js'
	export default {
		data() {
			return {
				isChecked: false,
				region: ['北京市', '北京市', '北京市'],
				isDis: false,
				addressName: '', //姓名
				addressTel: '', //电话
				addressData: "", //详细地址
				regionData: '', //省市区
				addressIsDefault: 0, //1默认
				addressId: ''
			}
		},
		onLoad(option) {
			if (option.id) {
				this.addressId = option.id
				this.get_address_data(option.id)
			}
		},
		methods: {
			//获取地址详情
			get_address_data(id) {
				let data = {
					id: id
				}
				this.Http.POST(get_address_data, data)
					.then((res) => {
						console.log('[收货地址]', res);
						if (res.code == 1) {
							let object = res.data
							this.addressName = object.contacts
							this.addressTel = object.contact_number
							this.region = object.address.split(",")
							this.regionData = object.address
							this.addressData = object.detailed_address
							this.addressIsDefault = object.switch
							if (this.addressIsDefault == 0) {
								this.isChecked = false
							} else {
								this.isChecked = true
							}
							console.log()
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},

			//选择地址
			RegionChange(e) {
				this.region = e.detail.value
				this.regionData = this.region.toString()
			},

			//收货地址
			getAddress() {
				let _ts = this.$tools.showToast
				if (this.addressName == '') {
					_ts('请填写收货人')
					return
				}
				if (this.addressTel == '') {
					_ts('请填写收货人手机号')
					return
				}
				if (this.regionData == '') {
					_ts('请选择收货地址')
					return
				}
				if (this.addressData == '') {
					_ts('请填写详细地址')
					return
				}
				let data = {}
				let url = ''
				if (this.addressId) {
					url = set_edit_addrees
					data = {
						id: this.addressId,
						contacts: this.addressName,
						contact_number: this.addressTel,
						address: this.regionData,
						detailed_address: this.addressData,
						switch: this.addressIsDefault
					}
				} else {
					url = set_address
					data = {
						contacts: this.addressName,
						contact_number: this.addressTel,
						address: this.regionData,
						detailed_address: this.addressData,
						switch: this.addressIsDefault
					}
				}
				this.Http.POST(url, data)
					.then((res) => {
						console.log('[收货地址]', res);
						if (res.code == 1) {
							this.$tools.showToast2(res.msg)
							setTimeout(t => {
								uni.navigateBack({
									delta: 1
								})
							}, 1000)
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},
			//地址
			changeAddressType(event) {
				console.log(event.detail.value);
				if (event.detail.value) this.addressIsDefault = 1
				else this.addressIsDefault = 0
			}
		}
	}
</script>

<style lang="scss">
	.address_from {
		display: flex;
		align-items: center;
		padding: 30rpx;
		background-color: white;

		.title {
			width: 140rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #262626;
			opacity: 1;
		}

		input {}

	}

	page {
		background: #F5F5F5;
	}

	.address_submit {
		width: 658rpx;
		height: 90rpx;
		border-radius: 99rpx;
		background: linear-gradient(180deg, #FF6B2B 0%, #FF8A19 100%);
		opacity: 1;
		line-height: 90rpx;
		text-align: center;
		color: white;
		position: fixed;
		z-index: 999;
		bottom: 60rpx;
	}
</style>
